package com.koron.device.service.propconfig;

import com.koron.device.bean.Equipment;
import com.koron.device.bean.EquipmentQuery;
import com.koron.device.mapper.EquipmentMapper;
import com.koron.device.mapper.TypeParamsMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: ManagerUnitConfig
 * @projectName GDH_EAM_V2
 * @description: 设备管理单位配置
 * @date 2022/2/1514:55
 */
@Component
public class ManagerUnitConfig implements PropConfig {

    @Override
    public List<String> sign() {
        return Arrays.asList("hasManagerUnit");
    }

    @Override
    public void setup(SessionFactory factory, List<Equipment> equipments, EquipmentQuery query) {
        // 临时存放环境
        String dbEnv = factory.getDbEnv();
        factory.setDbEnv("_default");
        TypeParamsMapper typeMapper = factory.getMapper(TypeParamsMapper.class);
        // 设备管理单位的参数id
        List<String> managerIds = typeMapper.getBasicParamByName("设备管理单位");
        factory.setDbEnv(dbEnv);
        EquipmentMapper mapper = factory.getMapper(EquipmentMapper.class);
        List<String> paramIds = mapper.countParam(equipments.stream().map(Equipment::getId).collect(Collectors.toList()), managerIds, null);
        // 如果不存在于列表中，则不存在
        equipments.stream().forEach(row -> row.setHasManagerUnit(paramIds.contains(row.getId())));
    }

    /**
     * 序号小的先执行
     * @return
     */
    @Override
    public int getOrder() {
        return 1;
    }
}
